package com.example.gw_back_end.service.mongodb;

import com.example.gw_back_end.domain.document.TokenDocument;
import com.example.gw_back_end.repository.TokenRepository;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class TokenService {

    private final TokenRepository tokenRepository;
    public TokenService(TokenRepository tokenRepository) {
        this.tokenRepository = tokenRepository;
    }

    // 存入TokenDocument对象
    public void insertToken(TokenDocument token) {
        tokenRepository.save(token);
    }

    // 根据token删除TokenDocument对象
    public void removeToken(String token) {
        tokenRepository.deleteById(token);
    }

    // 根据token获取TokenDocument对象
    public String getRawToken(String token) {
        Optional<TokenDocument> tokenDocument = tokenRepository.findById(token);
        return tokenDocument.map(TokenDocument::getRawToken).orElse(null);
    }
}